package com.example.smartcity_poverty.ui.Glide

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import com.bumptech.glide.Glide
import com.example.smartcity_poverty.App.Companion.context
import com.example.smartcity_poverty.App.Companion.isStart
import com.example.smartcity_poverty.MainActivity
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.util.Util.show
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import kotlinx.android.synthetic.main.activity_guilde.*

/**
 * App引导页
 */
class GuideActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 隐藏标题栏
        supportActionBar?.hide()
        setContentView(R.layout.activity_guilde)

        bannerGuide()
        guideNET.setOnClickListener {
            alert()
        }
    }

    /**
     * 引导页
     */
    private fun bannerGuide(){

        // 存储引导页图片
        val arrGuide = ArrayList<Int>().apply {
            this.add(R.mipmap.smartcity1)
            this.add(R.mipmap.smartcity2)
            this.add(R.mipmap.smartcity3)
            this.add(R.mipmap.smartcity4)
            this.add(R.mipmap.smartcity5)
        }


        // 使用Banner做引导页
        // 在xml布局添加app:banner_infinite_loop="false"这行，使轮播图不能无限循环
        guideBanner.apply {
            this.adapter = object : BannerImageAdapter<Int>(arrGuide){
                override fun onBindView(p0: BannerImageHolder?, p1: Int?, p2: Int, p3: Int) {
                    Glide.with(context).load(p1).into(p0?.imageView!!)
                }
            }
            // 显示指示器
            this.addBannerLifecycleObserver(this@GuideActivity).indicator = CircleIndicator(context)

            // 滑动监听
            this.addOnPageChangeListener(object : OnPageChangeListener{
                override fun onPageScrollStateChanged(p0: Int) {

                }

                override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

                }

                override fun onPageSelected(position: Int) {
                    if (position == 4){
                        guideNET.visibility = View.VISIBLE
                        guideOK.visibility = View.VISIBLE
                    }else{
                        guideNET.visibility = View.GONE
                        guideOK.visibility = View.GONE
                    }
                }

            })


            // 处理点击事件
            guideOK.setOnClickListener{
                getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val http = getString("http", "").toString()
                    if (http.isEmpty()){
                        "请先配置网络设置！".show()
                    }else{
                        getSharedPreferences("token", Context.MODE_PRIVATE)
                            .edit().putBoolean("true", true).apply()
                        startActivity(Intent(this@GuideActivity, MainActivity::class.java))
                        finish()
                    }
                }
            }
        }
    }


    /**
     * 网络配置
     */
    private fun alert(){
        runOnUiThread {
            // 获取布局
            val layout = View.inflate(this, R.layout.guidenet, null)
            // 获取控件
            val ed = layout.findViewById<EditText>(R.id.guideEditText)

            // 弹窗让用户填写服务器IP地址
            AlertDialog.Builder(this).apply {
                setTitle("配置IP地址")
                setView(layout)
                setPositiveButton("确定"){ _, _ ->
                    if (ed.text.toString().isEmpty()){
                        getSharedPreferences("token", Context.MODE_PRIVATE).edit().putString("http", "124.93.196.45:10001").apply()
                    }else{
                        getSharedPreferences("token", Context.MODE_PRIVATE).edit().putString("http", ed.text.toString()).apply()
                    }
                }
                setNegativeButton("取消"){ _, _ ->

                }
                // 显示
                show()
            }
        }

    }


    /**
     * 判断是否第一次
     */
    override fun onStart() {
        super.onStart()
        // 如果已经登录过， 侧直接进入主页
        if (isStart()){
            startActivity(Intent(context, MainActivity::class.java))
            finish()
        }
    }

    /**
     * 获取Http地址
     */
    companion object{
        fun getIP() : String{
            return context.getSharedPreferences("token", Context.MODE_PRIVATE).getString("http", "").toString()
        }
    }
}